<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="85px"
    >
      <el-form-item label="工作站名称" prop="workstationName">
        <el-input
          v-model="queryParams.workstationName"
          placeholder="请输入工作站名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="principalId">
        <el-select
          filterable
          v-model="queryParams.principalId"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in teachers"
            :key="item.teacherId"
            :label="item.name"
            :value="item.teacherId"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="$store.getters.roleId == 'schoolAdmin'"
        label="分院"
        prop="branchId"
      >
        <el-select
          filterable
          v-model="queryParams.branchId"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in branchList"
            :key="item.branchId"
            :label="item.branchName"
            :value="item.branchId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          v-preventReClick
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button
          v-preventReClick
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <div class="commonMenu">
      <el-menu
        :default-active="activeIndex"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
      >
        <el-menu-item index="0">全部</el-menu-item>
        <el-menu-item index="1">待提交</el-menu-item>
        <el-menu-item index="2">待分院审核</el-menu-item>
        <el-menu-item index="3">待学校审核</el-menu-item>
        <el-menu-item index="4">已驳回</el-menu-item>
        <el-menu-item index="5">已启用</el-menu-item>
        <el-menu-item index="6">已停用</el-menu-item>
      </el-menu>
    </div>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        fixed
        width="150"
        label="分院名称"
        align="center"
        show-overflow-tooltip
        prop="branchName"
      />
      <el-table-column
        fixed
        show-overflow-tooltip
        width="200"
        label="工作站名称"
        align="center"
        prop="workstationName"
      />
      <el-table-column label="状态" width="130" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag
            type="success"
            v-if="scope.row.status == 0 && scope.row.checkStatus == 0"
          >启用
          </el-tag
          >
          <el-tag type="danger" v-if="scope.row.status == 1">停用</el-tag>
          <el-tag v-if="scope.row.status == 0 && scope.row.checkStatus == 1"
          >待提交
          </el-tag
          >
          <el-tag
            type="warning"
            v-if="scope.row.status == 0 && scope.row.checkStatus == 2"
          >待分院审核
          </el-tag
          >
          <el-tag
            type="warning"
            v-if="scope.row.status == 0 && scope.row.checkStatus == 3"
          >待学校审核
          </el-tag
          >
          <el-tag
            type="danger"
            v-if="scope.row.status == 0 && scope.row.checkStatus == 4"
          >已驳回
          </el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        label="工作站地址"
        show-overflow-tooltip
        align="center"
        width="150"
        prop="workstationAddress"
      />
      <el-table-column
        label="关联课程数量"
        width="150"
        align="center"
        prop="courseIds"
      >
        <template slot-scope="scope">
          <span>{{
              scope.row.courseIds ? scope.row.courseIds.split(',').length : 0
            }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="容纳学生数量"
        width="150"
        align="center"
        prop="stuNum"
      />
      <el-table-column
        label="负责人"
        width="150"
        align="center"
        prop="principalName"
      />
      <el-table-column
        label="创建时间"
        width="170"
        align="center"
        prop="createTime"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评价" align="center" width="160">
        <template slot-scope="scope">
          <div style="text-align: left !important">
            <el-rate
              :value="scope.row.workstationStar"
              disabled
              show-score
              text-color="#ff9900"
              score-template="{value}"
            >
            </el-rate>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        width="180"
        class-name="small-padding fixed-width"
      >
        <!-- // 0.启用 1.禁用 2.待提交 3.待审核 4.已驳回 5.已到期 -->
        <template slot-scope="scope">
          <!-- 详情按钮  -->
          <el-button
            v-preventReClick
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
          >
            详情
          </el-button>
          <el-button
            v-preventReClick
            v-if="
              (scope.row.status == 0 &&
                scope.row.checkStatus == 2 &&
                $store.getters.roles.indexOf('branchAdmin') > -1) ||
              (scope.row.status == 0 &&
                scope.row.checkStatus == 3 &&
                $store.getters.roles.indexOf('schoolAdmin') > -1)
            "
            size="mini"
            type="text"
            icon="el-icon-open"
            @click="handleAudit(scope.row)"
          >
            审核
          </el-button>
          <!-- 更多下拉按钮  -->
          <el-dropdown
            v-if="
              (scope.row.status == 0 && scope.row.checkStatus == 4) ||
              (scope.row.status == 0 && scope.row.checkStatus == 0) ||
              scope.row.status == 1 ||
              scope.row.status == 1
            "
          >
            <span class="el-dropdown-link">
              <el-button slot="reference" type="text" size="mini">
                更多<i class="el-icon-arrow-down"></i>
              </el-button>
            </span>
            <el-dropdown-menu slot="dropdown">
              <!-- 驳回原因 按钮  -->
              <el-dropdown-item>
                <el-button
                  v-preventReClick
                  v-if="scope.row.status == 0 && scope.row.checkStatus == 4"
                  size="mini"
                  type="text"
                  icon="el-icon-thumb"
                  @click="handleReason(scope.row)"
                >
                  驳回原因
                </el-button>
              </el-dropdown-item>
              <!-- 停用|启用 按钮  -->
              <el-dropdown-item>
                <el-button
                  v-preventReClick
                  v-if="
                    (scope.row.status == 0 && scope.row.checkStatus == 0) ||
                    scope.row.status == 1
                  "
                  size="mini"
                  type="text"
                  icon="el-icon-open"
                  @click="handleSwitch(scope.row)"
                >
                  {{ scope.row.status == 0 ? '停用' : '启用' }}
                </el-button>
              </el-dropdown-item>
              <!-- 删除 按钮  -->
              <el-dropdown-item>
                <el-button
                  v-preventReClick
                  v-if="scope.row.status == 1"
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >
                  删除
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <reject :type="3" :open.sync="rejectOpen" :item="rejectform"></reject>
    <!-- 添加或修改岗位对话框 -->
    <workstationAddDia
      :type="type"
      :id="id"
      :auditStatus="auditStatus"
      :open.sync="open"
      @refresh="diaRefresh"
    ></workstationAddDia>
  </div>
</template>

<script>
import { listPost_branch } from '@/api/schoolOrganization/organization'
import {
  listPost_workstation,
  delPost_workstation,
  updatePost_workstation
} from '@/api/workstation/workstation'
import { listPost_teacher } from '@/api/schoolOrganization/teacher'
import workstationAddDia from '@/components/workstationAddDia'
import reject from '@/components/reject.vue'

export default {
  name: 'Workstation',
  components: {
    workstationAddDia,
    reject
  },
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      branchList: [],
      // 弹出层标题
      type: 0,
      auditStatus: '',
      // 是否显示弹出层
      open: false,
      id: 0,
      // level
      level: [],
      // 查询参数
      activeIndex: '0',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        branchId: undefined,
        principalId: undefined,
        workstationName: undefined,
        status: undefined,
        checkStatus: undefined
      },
      levels: [
        { id: 1, name: '级别一' },
        { id: 2, name: '级别二' }
      ],

      //   教师列表
      teachers: [],
      rejectOpen: false,
      rejectform: {}
    }
  },
  created() {
    this.getList()
    this.getTeacher()
    this.getBranch()
  },
  activated() {
    this.getList()
  },
  methods: {
    async getBranch() {
      let res = await listPost_branch({
        pageNum: 1,
        pageSize: 999,
        schoolId: this.$store.getters.schoolId
      })
      this.branchList = res.rows
    },
    /* 驳回原因 */
    handleReason(row) {
      this.rejectform = row
      this.rejectOpen = true
    },
    // 获取教师列表 teacherId name
    getTeacher() {
      listPost_teacher({
        pageNum: 1,
        pageSize: 999,
        schoolId: this.$store.getters.schoolId
      }).then((response) => {
        this.teachers = response.rows
      })
    },

    handleSelect(key) {
      switch (parseInt(key)) {
        case 0:
          this.queryParams.status = ''
          this.queryParams.checkStatus = ''
          break
        case 1:
          this.queryParams.status = 0
          this.queryParams.checkStatus = 1
          break
        case 2:
          this.queryParams.status = 0
          this.queryParams.checkStatus = 2
          break
        case 3:
          this.queryParams.status = 0
          this.queryParams.checkStatus = 3
          break
        case 4:
          this.queryParams.status = 0
          this.queryParams.checkStatus = 4
          break
        case 5:
          this.queryParams.status = 0
          this.queryParams.checkStatus = 0
          break
        case 6:
          this.queryParams.status = 1
          this.queryParams.checkStatus = 0
          break
      }
      this.getList()
    },
    // 审核按钮
    handleAudit(row) {
      this.open = true
      this.type = 3
      this.auditStatus = row.checkStatus
      this.id = row.workstationId
    },
    // 停用 启用按钮
    handleSwitch(row) {
      this.$confirm(
        `是否确认${row.status == 0 ? '停用' : '启用'}【${
          row.workstationName
        }】?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          let data = JSON.parse(JSON.stringify(row))
          data.status = row.status == 0 ? 1 : 0
          updatePost_workstation(data).then(() => {
            this.$modal.msgSuccess('操作成功')
            this.open = false
            this.getList()
          })
        })
        .catch(() => {
        })
    },
    diaRefresh() {
      this.getList()
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      this.queryParams.schoolId = this.$store.getters.schoolId
      listPost_workstation(this.queryParams).then((response) => {
        response.rows.map((item) => {
          if (item.workstationStar == undefined) {
            item.workstationStar = 0
          }
        })
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.workstationId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /* 详情按钮操作 */
    handleDetail(row) {
      this.$router.push({
        path: '/workstation/workstationDetail',
        query: {
          workstationId: row.workstationId
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const workstationId = row.workstationId || this.ids
      this.$modal
        .confirm(`是否确认删除【${row.workstationName}】？`)
        .then(function() {
          return delPost_workstation(workstationId)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/post/export',
        {
          ...this.queryParams
        },
        `post_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
<style scoped lang="scss">
::v-deep .el-menu-item:hover {
  border-bottom: 1px solid #e6e6e6 !important;
}

::v-deep .el-menu--horizontal > .el-menu-item.is-active:hover {
  border-bottom: 2px solid #11a983 !important;
}
</style>
